<template>
  <!-- 处理滚动穿透  此为官方推荐做法 https://uniapp.dcloud.net.cn/component/uniui/uni-popup.html#%E4%BB%8B%E7%BB%8D -->
  <page-meta :page-style="'overflow:' + (moveThrough ? 'hidden' : 'visible')"></page-meta>
  <div>
    <div class="msg-alert">
      <NetworkAlert />
    </div>
    <Message :sessionId="sessionId" />
  </div>
</template>

<script lang="ts" setup>
import { onPageScroll, onPullDownRefresh, onShow, onHide, onReady } from '@dcloudio/uni-app'
import { debounce } from '@/utils/tools'
import { ref, onMounted, onUnmounted } from 'vue'
import { onLoad, onUnload } from '@dcloudio/uni-app'
import NetworkAlert from '../../components/NetworkAlert.vue'
import Message from './message/index.vue'
import store from '@/store'
import bus from '@/utils/bus'

const title = ref('')
const scene = ref('')
let sessionId = ref('')

// 处理uni-popup 引起的滚动穿透
const moveThrough = ref(false)

onHide(() => {
  store.commit('im/VIDEO_STOP')
})

const clearUnread = (id) => {
  uni.$nim.V2NIMConversationService.clearUnreadCountByIds([id]).then((res) => {
    console.log('清除未读数成功')
  })
}

onLoad((e) => {
  console.log(e)
  sessionId.value = e.session
  const ids = e.session.split('|')
  scene.value = ids[1]

  const id = ids[2]
  if (scene.value == '1') {
    uni.$nim.V2NIMUserService.getUserList([id]).then((res) => {
      console.log('-------------', res)
      title.value = res[0].name

      uni.setNavigationBarTitle({
        title: title.value
      })
    })
  } else if (scene.value == '2') {
    uni.$nim.V2NIMTeamService.getTeamInfo(id, 1).then((res) => {
      console.log('-------------', res)
      title.value = res.name

      uni.setNavigationBarTitle({
        // title: title.value
        title: '健康咨询'
      })
    })
  }
  clearUnread(e.session)

  // const account = parseSessionId(sessionId)
  // scene = account.scene
  // to = account.to
  // uni.$on(events.HANDLE_MOVE_THROUGH, (flag) => {
  //   moveThrough.value = flag
  // })

  bus.on('clearUnread', () => {
    clearUnread(e.session)
  })
})

onUnmounted(() => {
  bus.off('clearUnread')
})

onMounted(() => {
  // setTimeout(() => {
  //   uni.$emit(events.ON_CHAT_MOUNTED)
  // }, 300)
  // if (scene === 'p2p') {
  //   title.value = deepClone(uni.$UIKitStore.uiStore.getAppellation({ account: to }))
  // } else if (scene === 'team') {
  //   const team = uni.$UIKitStore.teamStore.teams.get(to)
  //   const subTitle = `(${team?.memberNum || 0})`
  //   title.value = (team?.name || '') + subTitle
  // }
  // uni.setNavigationBarTitle({
  //   title: title.value,
  // })
})

// onUnload(() => {
//   uni.$off(events.CONFIRM_FORWARD_MSG)
//   uni.$off(events.CANCEL_FORWARD_MSG)
// })

// 触底刷新还是下拉刷新，您可根据scroll-view自行实现

// #ifndef H5
// onPullDownRefresh(debounce(() => {
//   uni.$emit(events.ON_LOAD_MORE)
// }, 1000))
// #endif

// #ifdef H5
// h5端下拉刷新会刷新页面，所以此处触顶加载更多消息
// onPageScroll(debounce(function (data: any) {
//   if (data.scrollTop <= 10) {
//     // uni.$emit(events.ON_REACH_TOP)
//     uni.$emit(events.ON_LOAD_MORE)
//   }
// }, 1000))
// #endif
</script>

<style scoped lang="scss">
.msg-alert {
  display: flex;
  flex-direction: column;
  width: 100%;
  position: fixed;
  z-index: 1;
}
</style>
